package com.example.newtown;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	private Context context;
	private AudioManager audioManager;
	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private static SoundManager soundManager;
	private static Object key = new Object();;
	private SoundManager() {}
	public static SoundManager getInstance(Context context) {
		if(soundManager == null)
			synchronized (key) {
				if(soundManager == null){
					soundManager = new SoundManager();
					soundManager.init(context);
				}
			}
			
		return soundManager;
	}
	private void init(Context context) {
		// TODO Auto-generated method stub
		this.context = context;
		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
		soundPoolMap = new HashMap<Integer, Integer>();
		audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
	}
	public void addSound(int soundKey, int resourceId) {
		int id = soundPool.load(context, resourceId, 1);
		soundPoolMap.put(soundKey, id);
	}
	public void play(int soundKey, boolean loop) {
		float volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		soundPool.play(soundPoolMap.get(soundKey), volume, volume, 1, loop ? -1 : 0, 0);
	}
}
